package com.pzh.assignment.common.utils;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.pzh.assignment.common.exception.UncheckedExecuteException;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @description: 该类默认忽略不存在的属性，时间格式为:yyyy-MM-dd HH:mm:ss
 * @author pengzhihao
 * @date 2022/4/3
 */
public class JacksonUtil {

	private static final ObjectMapper MAPPER = new ObjectMapper();

	static {
//		SimpleModule module = new SimpleModule("LowerCaseKeyDeserializer", Version.unknownVersion());
//		module.addKeyDeserializer(Object.class, new LowerCaseKeyDeserializer());
//		MAPPER.registerModule(module);
		MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		MAPPER.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
		MAPPER.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
		MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
		MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
		MAPPER.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false);
		MAPPER.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false);
		MAPPER.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
		MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
	}

    /**
     * 将java对象转换成json字符串
     *
     * @param obj 准备转换的对象
     * @return json字符串
     */
    public static String beanToJson(Object obj) {
        try {
        	return MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new UncheckedExecuteException(e);
        }
    }

    /**
     * 将json字符串转换成java对象
     *
     * @param json 准备转换的json字符串
     * @param cls 准备转换的类
     */
    public static <T> T jsonToBean(String json, Class<T> cls) {
        try {
        	T object = MAPPER.readValue(json, cls);
			return object;
        } catch (IOException e) {
            throw new UncheckedExecuteException(e);
        }
    }
    
    public static <T> T jsonToBean(String json, TypeReference<T> typeReference) {
        try {
        	T object = MAPPER.readValue(json, typeReference);
			return object;
        } catch (IOException e) {
            throw new UncheckedExecuteException(e);
        }
    }

}
